package ch.bfh.fbi.mobicomp.zap01;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

/**
 * Let us recapitulate on how to start something via BroadcastReceiver...
 * 
 * @author quantasy
 * 
 */
public class Zap01MessageBroadcastReceiver extends BroadcastReceiver {
	public static final String REMOTE_ZAPPER_ACTION = "ch.bfh.fbi.mobicomp.zap01.Zap01MessageBroadcastReceiver.REMOTE_ZAPPER_ACTION";
	public static final String ZAPPER_KEY_WORD = "ZAP:";
	private static final String destructionKey = "destroyMe";
	private static final String haltKey = "stopDestroyMe";

	private static final String TAG = "ZAP01";

	public static Intent getRemoteZapperMessageIntent(final String message) {
		Intent intent = null;
		if (message.startsWith(Zap01MessageBroadcastReceiver.ZAPPER_KEY_WORD)) {
			intent = new Intent(
					Zap01MessageBroadcastReceiver.REMOTE_ZAPPER_ACTION);
			intent.putExtra("message", message
					.substring(Zap01MessageBroadcastReceiver.ZAPPER_KEY_WORD
							.length()));
		}
		return intent;
	}

	@Override
	public void onReceive(final Context context, final Intent intent) {
		if (Zap01MessageBroadcastReceiver.destructionKey.equals(intent
				.getExtras().getString("message"))) {
			Log.d(Zap01MessageBroadcastReceiver.TAG, "destruction initiated...");
			Toast.makeText(context, "Self destruction sequence initiated.",
					Toast.LENGTH_LONG).show();

		} else if (Zap01MessageBroadcastReceiver.haltKey.equals(intent
				.getExtras().getString("message"))) {
			Log.d(Zap01MessageBroadcastReceiver.TAG, "destruction aborted...");
			Toast.makeText(context, "Self destruction sequence aborted.",
					Toast.LENGTH_LONG).show();
		} else {
			Log.d(Zap01MessageBroadcastReceiver.TAG, "unknown command.");
		}
	}
}
